package com.financing.wap.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstHotSearchLogDTO;
import com.financing.wap.service.ICommonDstService;
import com.financing.wap.service.IDstHotSearchLogService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.ArticleInfoVO;
import com.financing.wap.web.controller.BaseController;

@Controller("dstHotSearchLogController")
@RequestMapping("/api/dstHotSearchLog")
public class DstHotSearchLogController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstHotSearchLogService dstHotSearchLogService;
	
	@Autowired
	private ICommonDstService commonDstService;
	
	/***
	 * 热搜列表
	 * @param request
	 * @param dstHotSearchLog
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listDstHotSearchLog")
	@ResponseBody
	public ResultMap list(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstHotSearchLog list begin .......");
			
			
			ArticleInfoVO vo=new ArticleInfoVO();
			List<ArticleInfoVO> list = commonDstService.topSearch(vo);
			
			result.setAttribute("list",list);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstHotSearchLog list end .......");
		}catch(Exception e){
			log.error("query dstHotSearchLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 添加热搜
	 * @param request
	 * @param dstHotSearchLog
	 * @return
	 */
	@RequestMapping("/addDstHotSearchLog")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, DstHotSearchLogDTO dstHotSearchLog){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstHotSearchLog  begin .......");
			
			 String openid=request.getParameter("openid");
			 String userSearchKeyword=dstHotSearchLog.getUserSearchKeyword();
			 String searchArticleTitle=dstHotSearchLog.getSearchArticleTitle();
			 String searchArticleCode=dstHotSearchLog.getSearchArticleCode();
			//参数  默认
			if(StringUtil.isNullOrEmptyStr(openid,userSearchKeyword,searchArticleTitle,searchArticleCode)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			dstHotSearchLog.setUserOpenid(openid);
			dstHotSearchLog.setSearchDate(DateUtil.getDate());
			DTOdateSetUtil.setCreateInfo(dstHotSearchLog);
			DTOdateSetUtil.setUpdateInfo(dstHotSearchLog);
			
			int id = dstHotSearchLogService.insert(dstHotSearchLog);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstHotSearchLog list end .......");
		}catch(Exception e){
			log.error("add dstHotSearchLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
